const router = require('express').Router()
const adminModel = require("../../models/adminModel")
const aes = require('../../common/aes'); // 解密 / 解密
const Jwt = require('../../common/jwt'); // token


// 注册账号
// URL: http://localhost:3000/api/admin/register
// 方式：POST
// 参数：admin_name, admin_email, admin_phone, admin_pwd
router.post('/register', async (req, res) => {
    let { admin_name, admin_email, admin_phone, admin_pwd } = req.body;
    if (admin_name && admin_email && admin_phone && admin_pwd) {
        // 处理当前添加数据的时间
        let dt = new Date();
        let admin_createTime = dt.toLocaleDateString() + " " + (dt.toLocaleTimeString());
        // 对密码进行加密
        let admin_password = aes.aesEncrypt(admin_pwd);
        await adminModel.register(admin_name, admin_email, admin_phone, admin_password, admin_createTime)
            .then(
                content => {
                    res.send({ content })
                }
            )
            .catch(
                err => {
                    res.send({ err })
                }
            )
    } else {
        res.send({
            code: -1,
            msg: "缺少参数！",
        })
    }
})


// 登录账号
// URL: http://localhost:3000/api/admin/login
// 方式：POST
// 参数: admin_email, admin_pwd
router.post('/login', async (req, res) => {
    let { admin_email, admin_pwd } = req.body;
    if (admin_email && admin_pwd) {
        await adminModel.login(admin_email)
            .then(
                content => {
                    // res.send({ content })
                    // let { admin_pwd } = content;
                    // // res.send({ admin_pwd });
                    if (content.admin_pwd) {

                        // aes.aesDecrypt(输入的密码, 注册的密码);

                        // 获取用户ID
                        let id = content.id;
                        let token;
                        if (id) {
                            let jwt = new Jwt(id);
                            token = jwt.getToken();
                        }

                        let bool = aes.aesDecrypt(admin_pwd, content.admin_pwd);
                        if (bool) {
                            res.send({
                                code: 200,
                                admin_email,
                                msg: "登录成功！",
                                bool,
                                token,
                            })
                        } else {
                            res.send({
                                code: 404,
                                msg: "密码错误！",
                                bool,
                            })
                        }
                    } else {
                        res.send({
                            code: 404,
                            msg: "该账号不存在！",
                            content,
                        })
                    }
                }
            )
            .catch(
                err => {
                    res.send({ err })
                }
            )
    } else {
        res.send({
            code: -1,
            msg: "缺少参数！",
        })
    }
})


// 删除账号
router.post('/remove', async (req, res) => { })


// 获取登录用户的账号信息
// URL: http://localhost:3000/api/admin/info
// 方式：POST
// 参数: id ? toten
// router.post('/info', async (req, res) => {
//     // 获取 token 信息（临时身份、令牌）
//     let token = req.header.token || req.body.token;
//     let result = new Jwt(token).verifyToken();
//     if (result.code === 200) {
//         let admin_id = result.info.data;

//         // 获取登录用户信息
//         await adminModel.info(admin_id)
//             .then(
//                 content => {
//                     res.send({ content })
//                 }
//             )
//             .catch(
//                 err => {
//                     res.send({ err })
//                 }
//             )
//         // 登录有效 
//         res.send({
//             code: 200,
//             msg: "登录尚未失效！获取账号信息成功！",
//             // result
//             // admin_id,

//         })
//     } else {
//         // 登录失效
//         res.send({
//             code: 404,
//             msg: "登录失效，请重新登录！",
//         })
//     }
// })


router.post('/info', async (req, res) => {
    // 获取 token 信息（临时身份、令牌）
    let token = req.header.token || req.body.token;
    let result = new Jwt(token).verifyToken();
    if (result.code === 200) {
        let admin_id = result.info.data;

        // 获取登录用户信息
        try {
            const content = await adminModel.info(admin_id);
            res.send({
                code: 200,
                msg: "登录尚未失效！获取账号信息成功！",
                content: content
            });
        } catch (err) {
            res.send({
                code: 500,
                msg: "获取用户信息失败",
                err: err
            });
        }
    } else {
        // 登录失效
        res.send({
            code: 404,
            msg: "登录失效，请重新登录！"
        });
    }
});


// 修改头像 avatar
router.post('/avatar', async (req, res) => { })



module.exports = router;